﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace ECGWeb
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        static ECGService.ECGServiceClient serviceClient = new ECGService.ECGServiceClient();
        static ECGService.User[] userList;
        static string[] dropdownlistData1; // có thể là mảng các mã bệnh nhân hoặc mảng các mã bác sĩ cho dropdownlist;
        static string[] dropdownlistData2; // có thể là mảng các mã bệnh nhân hoặc mảng các mã bác sĩ cho dropdownlist2;
        static string[] permissionddl = new string[] { "3", "2", "1" };

        protected void Page_Load(object sender, EventArgs e)
        {           
            if (Session["username"] != null)
            {
                if (!this.Page.IsPostBack)
                {
                    loadData();
                    this.realidDDL.DataSource = dropdownlistData1;
                    this.realidDDL.DataBind();
                    this.realidDDL2.DataSource = dropdownlistData2;
                    this.realidDDL2.DataBind();
                }
            }
            else
                Response.Redirect("~/HomePage.aspx");

            this.gridview1.DataSource = userList;
            this.gridview1.DataBind();            
            //this.permissionDDL.DataSource = permissionddl;
            //this.permissionDDL.DataBind();
        }        

        public void loadData()
        {
            userList = serviceClient.getAllUser();
            dropdownlistData1 = serviceClient.getAllPatientID();
            dropdownlistData2 = serviceClient.getAllPatientID();            
            //this.realidDDL.DataSource = serviceClient.getAllPatientID();            
            //this.realidDDL.DataBind();
            //this.realidDDL2.DataSource = serviceClient.getAllPatientID();
            //this.realidDDL2.DataBind();            
        }

        [WebMethod]
        public static string addNewUser(string username, string pwd, int permission, string realID)
        {
            int count = serviceClient.addNewUser(username, pwd, permission, realID);
            if (count == 1)
            {                
                userList = serviceClient.getAllUser();
                return "true";
            }
            else
                return "false";
        }

        [WebMethod]
        public static string editUser(string username, string pwd, int permission, string realID)
        {
            int count = serviceClient.editUser(username, pwd, permission, realID);
            if (count == 1)
            {
                userList = serviceClient.getAllUser();
                return "true";
            }
            else
                return "false";
        }

        [WebMethod]
        public static string deleteUser(string username)
        {
            int count = serviceClient.deleteUser(username);
            if (count == 1)
            {
                userList = serviceClient.getAllUser();
                return "true";
            }
            else
                return "false";
        }

        [WebMethod]
        public static string loadUserData(string username)
        {
            string s ="";
            ECGService.User user = serviceClient.getUserByUsername(username);
            s += user.username + "|";
            s += user.password + "|";
            s += user.permission + "|";
            s += user.realID;                       
            return s;            
        }

        [WebMethod]
        public static string checkUser(string username)
        {
            if (serviceClient.getUserByUsername(username) != null)
                return "1";
            else
                return "0";
        }

        [WebMethod]
        public static string demoAjax()
        {
            return DateTime.Now.ToString();
        }

        protected void gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gridview1.PageIndex = e.NewPageIndex;
            this.gridview1.DataBind();
        }

        protected void permissionDDL_TextChanged(object sender, EventArgs e)
        {            
            switch (permissionDDL.SelectedItem.ToString())
            {
                case "1":
                    dropdownlistData1 = new string[] { "" };
                    break;
                case "2":
                    dropdownlistData1 = serviceClient.getAllDoctorID();
                    break;
                case "3":
                    dropdownlistData1 = serviceClient.getAllPatientID();
                    break;
            }
        }

        protected void permissionDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (permissionDDL.SelectedItem.Text)
            {
                case "1":
                    dropdownlistData1 = new string[] { "" };
                    this.realidDDL.DataSource = dropdownlistData1;
                    this.realidDDL.DataBind();
                    break;
                case "2":
                    dropdownlistData1 = serviceClient.getAllDoctorID();
                    this.realidDDL.DataSource = dropdownlistData1;
                    this.realidDDL.DataBind();
                    break;
                case "3":
                    dropdownlistData1 = serviceClient.getAllPatientID();
                    this.realidDDL.DataSource = dropdownlistData1;
                    this.realidDDL.DataBind();
                    break;
            }
        }

        protected void permissionDDL2_TextChanged(object sender, EventArgs e)
        {
            switch (permissionDDL2.SelectedItem.Text)
            {
                case "1":
                    dropdownlistData2 = new string[] { "" };
                    this.realidDDL2.DataSource = dropdownlistData2;
                    this.realidDDL2.DataBind();
                    break;
                case "2":
                    dropdownlistData2 = serviceClient.getAllDoctorID();
                    this.realidDDL2.DataSource = dropdownlistData2;
                    this.realidDDL2.DataBind();
                    break;
                case "3":
                    dropdownlistData2 = serviceClient.getAllPatientID();
                    this.realidDDL2.DataSource = dropdownlistData2;
                    this.realidDDL2.DataBind();
                    break;
            }
        }        
    }
}